package android.BigCity;




import java.util.Scanner;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

import android.activity.R;
/*
 * klasa vo koja se prikazuva lekcijata za ucenje
 * sodrzi tekst-dijalog i implementiran audio-player
 */
public class Lekcija extends Activity implements Runnable{
	
    int pom = 1;
    int resid;
    int vkupno;
    String name = "";
    MediaPlayer mediaPlayer;
    SeekBar sb;
    Thread currentThread; 
    //volatile boolean stop = false;
    //volatile Thread  runner;
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lekcija);	
		
		
		Bundle extras = getIntent().getExtras();
		if(extras != null)
		{
			name = extras.getString("ime");	//go zema imeto na selektiranata lekcija		
		}
		TextView lblNaslov = (TextView)findViewById(R.id.lblLekcija);
		lblNaslov.setText(name);
		TextView lblSodrzina = (TextView)findViewById(R.id.lblSodrzina);
		
		//postavuvanje na soodvetnite sodrzini za dijalogot i audioto
		if(name.equals("Episode 1 - Nice to meet you"))
		{
			lblSodrzina.setText(R.string.sodrzina1);	
			resid = R.raw.bcsw_s01_e01;	
		}
		else if(name.equals("Episode 2 - Get a piece of the action"))
		{
			lblSodrzina.setText(R.string.sodrzina2);
			resid = R.raw.bcsw_s01_e02;
		}
		else if(name.equals("Episode 3 - I have been kicked out"))
		{
			lblSodrzina.setText(R.string.sodrzina3);
			resid = R.raw.bcsw_s01_e03;	
		}
		else if(name.equals("Episode 4 - We have a mission"))
		{
			lblSodrzina.setText(R.string.sodrzina4);
			resid = R.raw.bcsw_s01_e04;
		}
		else if(name.equals("Episode 5 - Why are you looking so glum?"))
		{
			lblSodrzina.setText(R.string.sodrzina5);
			resid = R.raw.bcsw_s01_e05;
		}
		else if(name.equals("Episode 6 - He is a pest"))
		{
			lblSodrzina.setText(R.string.sodrzina6);
			resid = R.raw.bcsw_s01_e06;
		}		
				
		
		// rabota so media player-ot
		
		mediaPlayer = MediaPlayer.create(this, resid);	
						
		final ImageButton btnPlay = (ImageButton)findViewById(R.id.btnPlay);
		final ImageButton btnPause = (ImageButton)findViewById(R.id.btnPause);		
		final ImageButton btnStop = (ImageButton)findViewById(R.id.btnStop);		
		btnPlay.setImageResource(R.drawable.play);
		btnPause.setImageResource(R.drawable.stop);
		btnStop.setImageResource(R.drawable.pause);
		
		sb = (SeekBar)findViewById(R.id.sbProgres);
		
		btnPause.setEnabled(false);
		btnStop.setEnabled(false);
		sb.setEnabled(false);
		sb.setProgress(0);
		vkupno = mediaPlayer.getDuration();
		
		/*
		 * f-ja za startuvanje na audioto
		 */
		OnClickListener btnPlayOnClickListener = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(!mediaPlayer.isPlaying())
					{
						mediaPlayer.start();
						btnPause.setEnabled(true);
						btnStop.setEnabled(true);
						btnPlay.setEnabled(false);
						sb.setEnabled(true);
						sb.setMax(vkupno);
					}
				
				if(pom == 0)
				{	
					pom = 1;
					mediaPlayer.seekTo(0);
					mediaPlayer.start();
					btnPause.setEnabled(true);
					btnStop.setEnabled(true);
					btnPlay.setEnabled(false);
					sb.setEnabled(true);
				}				
			}
		};;;
		/*
		 * f-ja za pauziranje na audioto
		 */
		OnClickListener btnPauseOnClickListener = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mediaPlayer.isPlaying()){
					mediaPlayer.pause();
					btnPlay.setEnabled(true);
					btnPause.setEnabled(false);
					sb.setEnabled(false);
				}				
			}
		};;;
		/*
		 * f-ja za stopiranje na audioto
		 */
		OnClickListener btnStopOnClickListener = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mediaPlayer.pause();
				mediaPlayer.seekTo(0);
				pom = 0;
				btnPlay.setEnabled(true);
				btnPause.setEnabled(false);
				btnStop.setEnabled(false);
				sb.setEnabled(false);
			}
		};;;
		
		btnPlay.setOnClickListener(btnPlayOnClickListener);
		btnPause.setOnClickListener(btnPauseOnClickListener);
		btnStop.setOnClickListener(btnStopOnClickListener);	
		
		/*
		 * f-ja za rabota so SeekBar-ot
		 */
		sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				if(fromUser)
				{
					mediaPlayer.seekTo(progress);
					sb.setProgress(progress);
				}				
			}
		});
		
		//currentThread = new Thread(this);
		//currentThread.start();	
		
		
		
	}
/*
 *  kreiranje na meni
 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, Menu.FIRST, 0, "Back");
		menu.add(0, Menu.FIRST+1, 1, "Test yourself");		
		return true;		
	}

	@Override
	/*
	 * postavuvanje funkcionalnosti na menito
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		
		switch (item.getItemId()) {
		case (Menu.FIRST):{
			
			finish();
			return true;
		}
		case (Menu.FIRST+1):{
			
			
			Intent intent = new Intent(this,Prasanja.class);
			Bundle extras = new Bundle();
			extras.putString("ime", name);
			intent.putExtras(extras);
	    	
			startActivity(intent);
		}
		default:
			break;
		}
		
		return false;
	}

	@Override
	/*
	 * f-ja koj se izvrsuva pri preminuvanje kon drug ekran
	 */
	protected void onDestroy() {
		super.onDestroy();
		mediaPlayer.release();	
		//stop = true;
	}

/*	@Override
	protected void onPause() {
		super.onPause();
		stop = true;
		//currentThread.interrupt();
	}*/
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}


	/*@Override
	public void run() {
		while(!stop)
		{ 
			try{
			while(mediaPlayer != null){
				int currentPosition = mediaPlayer.getCurrentPosition();
				Message msg = new Message();
				msg.what = currentPosition;
				threadHandler.sendMessage(msg);
			}
			Thread.sleep(100);
		}catch (InterruptedException e) {
			// TODO: handle exception
			e.printStackTrace();
		}	
		}
	}
	private Handler threadHandler = new Handler(){
		public void handleMessage(Message msg){
			sb.setProgress(msg.what);
		}
	};*/
		
}
